<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Live\Controller;
use Think\Controller;

class RegController extends BaseController{

    /*
     * 根据判断结果
     * 1 存在号码并设置提醒
     * 2 不存在则发送验证码
     */
    public function telverify(){
    	$usertel = I('post.usertel');
    	$liveid = I('post.liveid');
    	$tel = D(C('TABLE_TEL'));
    	$where['tel_num'] = $usertel;
    	$result = $tel->where($where)->find();
    	if(!empty($result)){
			$tel_id = $result['id'];
			if(!$this->checklivetotel($tel_id,$liveid)){
				if($this->insertlivetotel($tel_id,$liveid)){
					echo 1;//设置成功
				}
			}else{
				echo 3; //存在号码并设置了提醒
			}
    	}else{
			//如果不存在电话则发送短信并保存记录，同时设置提醒
			$this->verifynote($usertel);
			echo 2;//发送验证码
    	}
    }

	/*
     * 校验验证码并设置提醒
     */
	public function checkverify(){
		$usertel = I('post.usertel');
		$verfnum = I('post.verfnum');
		$liveid = I('post.liveid');
		if($verfnum!=session('verftel')){
			session('verftel',null);
			echo -1; //验证码错误
		}else{
			$tel_id = $this->inserttel($usertel);
			if($tel_id && $tel_id>0){
				if($this->insertlivetotel($tel_id,$liveid)){
					echo 1;
				}
			}

		}
	}

	//检测当前的直播ID号是否已经设置过
	protected function checklivetotel($telid,$liveid){
		$remind = D(C('TABLE_REMIND'));
		$res = $remind->where(array('live_id'=>$liveid,'tel_id'=>$telid))->select();
		if($res){
			return true;
		}else{
			return false;
		}
	}

	//给当前直播加提醒
	protected function insertlivetotel($telid,$liveid){
		$remind = D(C('TABLE_REMIND'));
		$remind->live_id = $liveid;
		$remind->tel_id = $telid;
		$remind->remind_settime = date("Y-m-d H:i:m");
		$remind->usertoken = $_COOKIE['user_userinfo'];
		$res = $remind->add();
		if($res){
			return true;
		}else{
			return false;
		}
	}

	//新增电话
	protected function inserttel($telnum){
		$tel = D(C('TABLE_TEL'));
		$tel->tel_num = $telnum;
		$tel->tel_regdate = date("Y-m-d H:i:m");
		$res = $tel->add();
		if($res){
			return $res;
		}else{
			return false;
		}
	}



}